package com.zhouxiaoxuan13.promotionservice.dto;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 促销详情DTO - 包含商品信息的完整促销数据
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PromotionDetailDTO {
    
    // 促销信息
    private Long id;
    private String name;
    private String description;
    private BigDecimal promotionPrice;
    private LocalDateTime startDate;
    private LocalDateTime endDate;
    private Boolean active;
    private LocalDateTime createdTime;
    private LocalDateTime updatedTime;
    
    // 关联的商品信息
    private Long freshProductId;
    private String productName;
    private String productDescription;
    private String category;
    private String specifications;
    private BigDecimal originalPrice;
    private Integer stockQuantity;
    
    // 计算字段
    private BigDecimal discountAmount;  // 优惠金额
    private BigDecimal discountRate;    // 优惠幅度（百分比）
    
    /**
     * 计算优惠金额
     */
    public BigDecimal getDiscountAmount() {
        if (originalPrice != null && promotionPrice != null) {
            return originalPrice.subtract(promotionPrice);
        }
        return BigDecimal.ZERO;
    }
    
    /**
     * 计算优惠幅度（百分比）
     */
    public BigDecimal getDiscountRate() {
        if (originalPrice != null && promotionPrice != null && originalPrice.compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal discount = originalPrice.subtract(promotionPrice);
            return discount.divide(originalPrice, 4, BigDecimal.ROUND_HALF_UP)
                          .multiply(new BigDecimal("100"));
        }
        return BigDecimal.ZERO;
    }
    
    /**
     * 判断促销是否当前有效
     */
    public Boolean isCurrentlyActive() {
        if (!active) return false;
        
        LocalDateTime now = LocalDateTime.now();
        return startDate != null && endDate != null 
               && !now.isBefore(startDate) 
               && !now.isAfter(endDate);
    }
}
